//
//  HJComposeView.swift
//  HJWeiBo
//
//  Created by 夏航军 on 16/10/6.
//  Copyright © 2016年 夏航军. All rights reserved.
//

import UIKit
import pop

class HJComposeView: UIView {

    //设置数组用于存储Button
   lazy var btnArray = [HJComposeButton]()
    //记录控制器.用于点击button是模态
    var targetVC : UIViewController?
    
    
    
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
       
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //传递控制器
    func show(target : UIViewController) {
        self.targetVC = target
        //把当前视图添加到控制器中
        target.view.addSubview(self)
    }
    
    
    
    //点击屏幕时图片消失
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        for value in btnArray.reversed().enumerated() {
            startAnimation(btn: value.element, index: value.offset, isUp: false)
        }
    }
    
    
    //从一个父视图上移到另一个父视图上时会调用该方法
    override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        
        //
        for value in btnArray.enumerated() {
            startAnimation(btn: value.element, index: value.offset)
        }
        
    }
    
    
    
    
    func setupUI() {
        //制造高斯模糊
        //第一种方法
//        let toolBar = UIToolbar(frame: UIScreen.main.bounds)
//        toolBar.barStyle = .black
//        self.addSubview(toolBar)
        
        //第二种方法
        let backView = UIImageView(image:UIImage.snapShotCurrentScreen().applyLightEffect())
        
        self.addSubview(backView)
        
        
        //设置图片背景文字
        let slogan = UIImageView(image: #imageLiteral(resourceName: "compose_slogan"))
        self.addSubview(slogan)
        
        slogan.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(100)
        }
        
        //添加微博类型按钮
        addChildButtons()
    }
    
    
    private func addChildButtons() {
        
        let margin = (KScreenW - 3 * composeBtnW)/(3 + 1)
        for i in 0..<6 {
            
            let btn = HJComposeButton()
           
            //计算X和Y
            let colIndex = i % 3
            let rowIndex = i / 3
            
            let btnX = margin + (composeBtnW + margin) * CGFloat(colIndex)
            let btnY = margin + (composeBtnH + margin) * CGFloat(rowIndex) + KScreenH
            
            btn.frame = CGRect(x: btnX, y: btnY, width: composeBtnW, height: composeBtnH)
            btn.setTitle("相机", for: .normal)
            btn.setImage(#imageLiteral(resourceName: "tabbar_compose_camera"), for: .normal)
            btn.sizeToFit()
            btn.addTarget(self, action: #selector(composeTypeBtnDidClick(btn:)), for: .touchUpInside)
            
            self.addSubview(btn)
            
            btnArray.append(btn)
        }
        
    }
    
    //button的点击事件
    func composeTypeBtnDidClick(btn:UIButton) {
        
        UIView.animate(withDuration: 0.5, animations: { 
            
            for composeBtn in self.btnArray {
                composeBtn.alpha = 0.1
                if composeBtn == btn {
                    //放大
                    composeBtn.transform = CGAffineTransform(scaleX: 2, y: 2)
                }else{
                    //缩小
                    composeBtn.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
                }
            }
            
            }) { (_) in
                //跳转页面 如果交给服务器配置 就只能够获取到控制器的名称的字符串
                //1. 从数据中解析需要跳转的控制器的类名(字符串)
                let productName = Bundle.main.infoDictionary!["CFBundleName"] as! String
                
                let className = productName + "." + "HJComposeViewController"
                
                //2.根据字符串获取类型
                let type = NSClassFromString(className) as! UIViewController.Type
                
                //3.根据类型实例化
                let vc = type.init()
                
                //将vc作为根控制器
                let nav = UINavigationController(rootViewController: vc)
                //4.跳转到目标控制器
                self.targetVC?.present(nav, animated: true, completion: {
                    //跳转是并且移除当前视图
                    self.removeFromSuperview()
                })
        }
    }
    
    
    //设置动画效果
    func startAnimation(btn : HJComposeButton , index : Int , isUp : Bool = true) {
        //实例化弹簧效果,并且指定哪个属性要做弹簧效果kPOPViewCenter = @"view.center";
        let anim = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        //设置动画对象的基本属性
        anim?.springBounciness = 10
        anim?.springSpeed = 12
        //设置动画时间
        anim?.beginTime = CACurrentMediaTime() + Double(index) * 0.025
        
        //停止时候的值
        anim?.toValue = NSValue.init(cgPoint: CGPoint(x: btn.center.x, y: btn.center.y + (isUp == true ? -350 : 350)))
        //添加到按钮上
        btn.pop_add(anim, forKey: nil)
    }
}







